package com.infore.remoteControl.service.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.infore.remoteControl.constant.Constants;
import com.infore.remoteControl.constant.InstructionConstants;
import com.infore.remoteControl.constant.ResultConstants;
import com.infore.remoteControl.entity.instructionsEntity.InstructionsData;
import com.infore.remoteControl.enums.ResultStatus;
import com.infore.remoteControl.repository.IInstructionDataRepository;
import com.infore.remoteControl.select.QueryDispatcher;
import com.infore.remoteControl.select.dto.QueryParamDto;
import com.infore.remoteControl.select.util.QueryUtil;
import com.infore.remoteControl.service.InstructionsDataService;
import com.infore.remoteControl.util.MyBeanUtils;
import com.infore.remoteControl.util.PageUtil;
import com.infore.remoteControl.util.ResultEntity;

@Service
public class InstructionsDataServiceImpl implements InstructionsDataService {
	
	@Autowired
	QueryDispatcher queryDispatcher;
	
	@Autowired
	IInstructionDataRepository iInstructionDataRepository;

	@Override
	public ResultEntity query(QueryParamDto queryParamDto) {
		if (queryParamDto == null) { // 无查询条件
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS)
					.setDataList(iInstructionDataRepository.findByOrderByState());
		} else if (queryParamDto.getPageObject() != null && queryParamDto.getQueryCondition() == null
				&& queryParamDto.getAssembleCondition() == null && queryParamDto.getAssembleSql() == null) { // 无查询条件只分页
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(PageUtil.pageConvert(
					iInstructionDataRepository.findByOrderByState(queryParamDto.getPageObject().getPageRequest())));
		} else if (StringUtils.isBlank(queryParamDto.getAssembleSql().getSelect())) { // 不指定任何字段
			return this.queryAll(queryParamDto);
		}
		return queryDispatcher.dispatcher(queryParamDto, InstructionsData.class);
	}

	@Override
	public ResultEntity edit(List<InstructionsData> instructionsDataList) {
		if (instructionsDataList != null && instructionsDataList.size() != 0) {
			for (InstructionsData instructionsData : instructionsDataList) {
				if (StringUtils.isNotBlank(instructionsData.getInstructionsDataId())) {
					//查询原有的对象
					InstructionsData oldInstructionData = iInstructionDataRepository.findById(instructionsData.getInstructionsDataId()).orElse(null);
					//判断对象是否存在
					if (oldInstructionData != null) {
						MyBeanUtils.copyNonNullProperties(instructionsData,oldInstructionData);
						instructionsData = oldInstructionData;
					} else {
						return new ResultEntity(ResultStatus.ERROR, ResultConstants.NO_SUCH_OBJECT);
					}
				}
				//验证指令
				ResultEntity resultEntity = validate(instructionsData);
				if (resultEntity.getCode() == 1) {
					return resultEntity;
				}
			}
			//保存或者更新数据
			iInstructionDataRepository.saveAll(instructionsDataList);
		} else {
			return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS);
	}

	@Override
	public ResultEntity delete(String ids) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultEntity validate(InstructionsData instructionsData) {
		if ("".equals(instructionsData.getEquipmentId()) || instructionsData.getEquipmentId() ==null) {
			return new ResultEntity(ResultStatus.ERROR,InstructionConstants.INSTRUCTIONDATA_EQUIPMENT_NULL);
		}else if (instructionsData.getState().equals("") || instructionsData.getState() ==null) {
			return new ResultEntity(ResultStatus.ERROR,InstructionConstants.INSTRUCTIONDATA_STATE_NULL);
		}
		return new ResultEntity(ResultStatus.SUCCESS,InstructionConstants.INSTRUCTION_SUCCESS);
	}
	
	// 不指定字段的通用查询
	@SuppressWarnings("serial")
	public ResultEntity queryAll(QueryParamDto queryParamDto) {
		return queryParamDto.getPageObject() != null // 判断是否分页
				? ResultEntity.page(iInstructionDataRepository.findAll(new Specification<InstructionsData>() {
					@Override
					public Predicate toPredicate(Root<InstructionsData> root, CriteriaQuery<?> query,
							CriteriaBuilder criteriaBuilder) {
						// 构建查询条件
						List<Predicate> predicates = QueryUtil.createQueryCondition(queryParamDto, criteriaBuilder,
								root);
						// 组装查询条件
						Predicate predicate = QueryUtil.assembleCondition(queryParamDto, criteriaBuilder, predicates);
						// 组装SQL
						QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
						return query.getRestriction();
					}
				}, queryParamDto.getPageObject().getPageRequest()))
				: new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS)
						.setDataList(iInstructionDataRepository.findAll(new Specification<InstructionsData>() {
							@Override
							public Predicate toPredicate(Root<InstructionsData> root, CriteriaQuery<?> query,
									CriteriaBuilder criteriaBuilder) {
								// 构建查询条件
								List<Predicate> predicates = QueryUtil.createQueryCondition(queryParamDto,
										criteriaBuilder, root);
								// 组装查询条件
								Predicate predicate = QueryUtil.assembleCondition(queryParamDto, criteriaBuilder,
										predicates);
								// 组装SQL
								QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
								return query.getRestriction();
							}
						}));
	}
}
